<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script type="text/javascript" src="../../js/jquery-3.2.1.js"></script>
    <script type="text/javascript" src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
  </head>
  
  <body>
    <div class="x-body">
      
       <table class="layui-hide" id="test" lay-filter="test"></table>
    </div>
    <script type="text/html" id="toolbarDemo">
			<a class="layui-btn layui-btn-sm" lay-event="uploadFile">上传文件</a>
            <a class="layui-btn layui-btn-sm" lay-event="reload">刷新</a>
		</script>
		<script type="text/html" id="barDemo">
	  	<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="download">下载</a>
			<a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
		</script>

    </div>
    <script type="text/javascript">
			layui.use(['table', 'layer'], function() {
					var layer = layui.layer;
					var table = layui.table;
					table.render({
					    elem: '#test'
					    ,height: 605
					    ,url: '../findFile' //数据接口
					    ,where:{'enable':1}
					    ,page: {limits:[10, 20, 30]} //开启分页
					    ,toolbar: '#toolbarDemo'  //开启工具栏
					    ,request: {
					   		pageName: 'pageNo' //页码的参数名称，默认：page
					    	,limitName: 'pageSize' //每页数据量的参数名，默认：limit
					  	}
					    ,cols: [[ //表头
					      {field: 'fileId', title: 'ID', width:80, sort: true, fixed: 'left'}
					      ,{field: 'fileName', title: '文件名', width:300,  fixed: 'left'}
					      ,{field: 'uploadTime', title: '上传时间', width:120, sort: true
					    	   ,templet:function(d){
					    		   return toMyDateString(d.uploadTime);
					    	   }	    
					      }
					      ,{fixed: 'right', title: '操作', width: 120,templet:'#barDemo'}
					    ]]
				   });
				    table.on('toolbar(test)', function(obj){
              var data = obj.data;
                if(obj.event === 'uploadFile'){
                    x_admin_show('上传文件','./fileUpload.html');
                } else (obj.event === 'reload'){
                	location.reload();
                }
          });   
        
         //监听行工具事件
    table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'download'){
            	var config = { 'url' : '../download', 'method' : 'post', 'data' : { 'fileId' : data.fileId } };
                var $iframe = $('<iframe id="down-file-iframe" />');
                var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
                $form.attr('action', config.url);
                for (var key in config.data) {
                    $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
                }
                $iframe.append($form);
                $(document.body).append($iframe);
                $form[0].submit();
                $iframe.remove();
            	
            }else if(obj.event === 'del'){
            	$.ajax({
            		type:"POST",
            		url:"../deleteFile",
            		async:true,
            		data:{'fileId' : data.fileId},
            		success:function(res){
            			if(res.msg=='success'){
            				layer.alert('删除成功' ,{icon: 1},function(){
            					location.reload();
            				});
            			}else{
            				layer.alert('删除失败' ,{icon: 2});
            			}
            		},
            		error:function(){
            			layer.alert('删除失败' ,{icon: 2});
            		}
            	});
            }
      
    		});
      });
      
      	function toMyDateString(timeMillis){
                var time = new Date(timeMillis);
                var month = time.getMonth()+1;
                if(month<10) month='0'+month;
                var day = time.getDate();
                if(day<10) day='0'+day;
                return time.getFullYear()+"-"+month+"-"+day;;
            }
    </script>

  </body>

</html>